package com.sgb.kjwl.view.ui.fragment.home;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.lxj.xpopup.interfaces.OnClickOutsideListener;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FrDisplaySettingsConfigBinding;
import com.sgb.kjwl.view.ui.activity.home.EnterpriseConfigActivity;
import com.sgb.kjwl.view.ui.activity.self.authentication.CertificationActivity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;

public class DisplaySettingsConfigFragment extends AppLazyFragment {

    private FrDisplaySettingsConfigBinding binding;
    private boolean isType = true;
    private boolean isQY = true;

    public static DisplaySettingsConfigFragment newInstance() {
        DisplaySettingsConfigFragment dialog = new DisplaySettingsConfigFragment();
        Bundle bundle = new Bundle();
        dialog.setArguments(bundle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fr_display_settings_config, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        binding.ivQyButton.setOnClickListener(this::onClick);
        binding.ivTypeButton.setOnClickListener(this::onClick);
        binding.tvClean.setOnClickListener(this::onClick);
        binding.tvSave.setOnClickListener(this::onClick);
    }

    @Override
    protected void onLazyLoad() {
        isType = MkvUtils.get().getBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsType,true);
        isQY = MkvUtils.get().getBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsQY,true);
        binding.ivQyButton.setChecked(isQY);
        binding.ivTypeButton.setChecked(isType);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.iv_qy_button){
            isQY = !isQY;
            initImageView(binding.ivQyButton,isQY);
        }else if (v.getId() == R.id.iv_type_button){
            isType = !isType;
            initImageView(binding.ivTypeButton,isType);
        }else if (v.getId() == R.id.tv_clean){
//            if (isChange()){
                showDialog();
//            }
        }else if (v.getId() == R.id.tv_save){
//            if (isChange()){
                MkvUtils.get().putBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsType,isType);
                MkvUtils.get().putBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsQY,isQY);
                isChange();
//                MToast.showToast(getActivity(),"您本次设置保存成功");
                getActivity().finish();
//            }else{
//                MToast.showToast(getActivity(),"您未做修改不用保存");
//            }

        }
    }

    private void initImageView(CheckBox checkBox, boolean isSelect){
        checkBox.setChecked(isSelect);
        isChange();
    }

    public void showDialog(){
        new RoundDialog.Builder(getActivity())
                .title("提示")
                .content("确定要退出吗？已更改的内容将不会保存!")
                .cancel("取消")
                .confirm("确认")
                .contentGravity(Gravity.CENTER)
                .cancelable(false)
                .cancelCallback(v -> {
//                    MkvUtils.get().putBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsType,isType);
//                    MkvUtils.get().putBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsQY,isQY);
//                    isChange();
//                    MToast.showToast(getActivity(),"本次设置保存成功");
                })
                .confirmCallback(v -> {
                    onLazyLoad();
                    isChange();
                    closeDialog();
                })
                .show();
    }

    /**
     * 检查设置是否更改，并在父布局同步
     */
    public boolean isChange(){
        if (getActivity() instanceof EnterpriseConfigActivity){
            EnterpriseConfigActivity activity = (EnterpriseConfigActivity)getActivity();
            if (MkvUtils.get().getBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsType,true) != isType ||
                    MkvUtils.get().getBoolean(MkvUtils.CommomData, CacheMarker.DisplaySettingsQY,true) != isQY){
                activity.isSettingChange = true;
                return true;
            }else{
                activity.isSettingChange = false;
            }
        }
        return false;
    }

    /**
     * 更改滚动配置并关闭Activity
     *
     * */
    private void closeDialog(){
        if (getActivity() instanceof EnterpriseConfigActivity){
            EnterpriseConfigActivity activity = (EnterpriseConfigActivity)getActivity();
            activity.finish();
        }
    }
}